define(function(require,exports,module) {
	var myPub = require('./public');
    var priceCount = require('./submit_calcLastPrice');

    var oUseGiftBtn = $('.js_useGiftBtn');
    var oSetNum = $('.js_setNum');
    var oUseMoneyBtn = $('.js_useMoneyBtn'); 	//使用按钮
    var oInputMoney = $('.js_inputMoney');		//输入框
    var oUseMoneytxt = $('.js_useMoneytxt');	//最终金额显示位置

    var oSumPrice = oUseGiftBtn.find('.js_sumGiftPrice'); 	//剩余礼品卡的金额
    var sBtnName = '去使用';


	/************************************** 使用礼品卡代码 ***********************************************/

	function getMaxCanUseGiftPrice(){
		var sumGift = parseFloat(oSumPrice.text());
		var maxPrice = priceCount.getMaxGiftPrice();
        return Math.min(sumGift,maxPrice);
	}

    //开启和关闭礼品卡使用
    oUseGiftBtn.hammer().on('tap',function() {
        if($(this).hasClass('on')) {
            $(this).removeClass('on').find('.js_arrow').removeClass('rotate90');
            oSetNum.hide();
        } else {
            $(this).addClass('on').find('.js_arrow').addClass('rotate90');
            oSetNum.show();
        }
    });


    //点击使用按钮执行 .toFixed(2)
    oUseMoneyBtn.hammer().on('tap', function () {
        oInputMoney.blur();
        checkMoneyInput(oInputMoney,function() {
            myPub.TipLayer('只能输入数字或者2位小数')
        },function() {
            var maxPrice = getMaxCanUseGiftPrice();
            var inputPrice = parseFloat(oInputMoney.val());
            if(inputPrice < 0 ) {
                oInputMoney.val(0);
                oUseMoneytxt.text(sBtnName).removeClass('color_red');
                myPub.TipLayer('输入的数字或小数必须大于0');
                return;
            }
            if(inputPrice > maxPrice){
                inputPrice = maxPrice;
            }
            oInputMoney.val(inputPrice);
            oUseMoneytxt.text('-￥'+ inputPrice).addClass('color_red');
            oUseGiftBtn.removeClass('on').find('.js_arrow').removeClass('rotate90');
            oSetNum.hide();
            priceCount.useGiftMoney(inputPrice);
        })
    });

    //
    oInputMoney.on('input propertychange', function () {
        var _val = parseFloat(oInputMoney.val());
        var maxPrice = getMaxCanUseGiftPrice();
        if(_val < 0 ) {
            oInputMoney.val(0);
            oUseMoneytxt.text(sBtnName);
            return;
        }
        if( _val > maxPrice) {
            oInputMoney.val(maxPrice);
        }
        var _reval = oInputMoney.val().replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
        if(oInputMoney.val().length > _reval.length) {
            oInputMoney.val(_reval)
        }
    });

    /*输入框验证*/
    function checkMoneyInput(obj,fn1,fn2){
        var oIphone = obj.val();
        var reg = /^[0-9]+(.[0-9]{1,2})?$/;   //验证 正2位小数
        if (!reg.test(oIphone) ){
            if(fn1)fn1();
        }
        else {
            if(fn2)fn2();
        }
    }

});